package com.lyj.controller;

import com.lyj.pojo.*;
import com.lyj.service.AdminService;
import com.lyj.service.NurseService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class NurseController {

    @Autowired
    NurseService nurseService;
    @Autowired
    AdminService adminService;
//登录
    /*@RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public List login(@Param("username")String username,
                       @Param("password") String password,
                       @Param("duty") String duty,
                       HttpSession session) {

        List list = new ArrayList();
        if (duty.equals("护工")) {
            Nurse login = nurseService.login(username, password, duty);
            if (login != null) {
                list.add(login);
                session.setAttribute("userid", login.getNurseId());
                session.setAttribute("loginname", username);
                session.setAttribute("duty", duty);
            }
            System.out.println("---护工---"+list);
            return list;
        }
        if (duty.equals("管理员")) {
            Admin login = adminService.adminLogin(username, password, duty);
            if (login != null) {
                list.add(login);
                session.setAttribute("userid", login.getAdminId());
                session.setAttribute("loginname", username);
                session.setAttribute("duty", duty);
            }
            return list;
        }
        list.add("false");
        return list;
    }*/
//更新护士个人信息
    @RequestMapping(value = "/updateNurseById",method = RequestMethod.POST)
    @ResponseBody
    public Boolean updateNurseById(Nurse nurse){
        Boolean aBoolean = nurseService.updateNurseById(nurse);
        return aBoolean;
    }
    //查看当前管理的老人信息
    @RequestMapping(value = "/queryOldManByNurseId",method = RequestMethod.POST)
    @ResponseBody
    public List<OldMan> queryOldManByNurseId(int nurseId){
        List<OldMan> oldManList = nurseService.queryOldManByNurseId(nurseId);
        System.out.println("-------"+oldManList);
        return oldManList;

    }
    //登录用户查看薪资

    @RequestMapping(value = "/querySalaryByNurseId",method = RequestMethod.POST)
    @ResponseBody
    public List<NurseSalary> querySalaryByNurseId(int nurseId){
        List<NurseSalary> nurseList = nurseService.querySalaryByNurseId(nurseId);
        System.out.println("-------"+nurseList);
        return nurseList;

    }

//添加请假
    @RequestMapping(value = "/vacation",method = RequestMethod.POST)
    @ResponseBody
    public Boolean vacation(int nurseId,String username,String startDate,String endDate,String day,String reason,String remarks) throws ParseException {


        Vacation vacation = new Vacation();
        vacation.setNurseId(nurseId);
        vacation.setUsername(username);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate2 = format.parse(startDate);
        Date endDate2 = format.parse(endDate);

        vacation.setStartDate(startDate2);
        vacation.setEndDate(endDate2);
        vacation.setDay(day);
        vacation.setReason(reason);
        vacation.setRemarks(remarks);
        Boolean aBoolean = nurseService.vacation(vacation);
        return aBoolean;

    }
//登录用户查看全部请假
    @RequestMapping(value = "/queryAllByNurseId",method = RequestMethod.POST)
    @ResponseBody
    public List<Vacation> queryAllByNurseId(int nurseId){


        List<Vacation> vacationList = nurseService.queryAllByNurseId(nurseId);
        return vacationList;

    }
//修改请假信息
@RequestMapping(value = "/updateVacation",method = RequestMethod.POST)
@ResponseBody
public Boolean updateVacation(int vacationId,int nurseId,String startDate,String endDate,String day,String reason,String remarks) throws ParseException {


    Vacation vacation = new Vacation();
    vacation.setVacationId(vacationId);
    vacation.setNurseId(nurseId);

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date startDate2 = format.parse(startDate);
    Date endDate2 = format.parse(endDate);

    vacation.setStartDate(startDate2);
    vacation.setEndDate(endDate2);
    vacation.setDay(day);
    vacation.setReason(reason);
    vacation.setRemarks(remarks);
    Boolean aBoolean = nurseService.updateVacation(vacation);
    return aBoolean;

}

//查询所有护工为空的老人
    @RequestMapping("queryOldManNurseNull")
    @ResponseBody
    public List<OldMan> queryOldManNurseNull(){

        List<OldMan> oldManNullList = nurseService.queryOldManNurseNull();
        return oldManNullList;

    }


}
